package com.crm.service.impl;

import com.crm.dao.RoleMapper;
import com.crm.domain.DataGridView;
import com.crm.pojo.Role;
import com.crm.service.RoleService;
import com.crm.vo.RoleVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public List<Role> findAllRoles() {
        return roleMapper.selectAllRoles();
    }

    @Override
    public List<Role> selectRolesByUid(int uid) {
        return this.roleMapper.selectRolesByUid(uid);
    }
    @Override
    public DataGridView findAllRoles(RoleVo roleVo) {
        Page<Object> page= PageHelper.startPage(roleVo.getPage(),roleVo.getLimit());
        List<Role> roleList=this.roleMapper.selectAllRoles();
        return new DataGridView(page.getTotal(),roleList);
    }

    @Override
    public boolean doAssignMenuByRoleId(Integer rid, Integer[] menuIds) {
        boolean flag=false;
        try {
            this.roleMapper.deleteRoleMenu(rid);
            for(Integer menuId:menuIds){
                Map<String,Integer> params=new HashMap<String,Integer>();
                params.put("roleId",rid);
                params.put("menuId",menuId);
                this.roleMapper.addRoleMenu(params);
            }
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
